package com.gxa.jjty.phy.service.impl;

import com.gxa.jjty.core.common.ExceptionCode;
import com.gxa.jjty.core.exception.BusinessException;
import com.gxa.jjty.core.pojo.PhyUser;
import com.gxa.jjty.core.pojo.Torder;
import com.gxa.jjty.core.service.impl.BaseServiceImpl;
import com.gxa.jjty.core.util.OrderIdUtil;
import com.gxa.jjty.phy.mapper.PhyUserMapper;
import com.gxa.jjty.phy.mapper.TorderMapper;
import com.gxa.jjty.phy.service.ToderService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service("toderService")
@Transactional(rollbackFor = Throwable.class)
public class ToderServiceImpl extends BaseServiceImpl<Torder> implements ToderService {

    @Autowired
    TorderMapper torderMapper=null;

    private static final String TORDER_NUM_PRE="CJR";

    @Override
    public  List<Torder>  selectByNameOrServer(String search,Integer orderState,Long phyId) throws BusinessException {
        List<Torder> torders = null;
        if(orderState==1){
            Example example  = new Example(Torder.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.orLike("orderNumber",search);
            criteria.andLike("orderState","等待系统分配");
            criteria.orLike("orderUser","%"+search+"%");
            criteria.andLike("orderState","等待系统分配");
            criteria.orLike("serveType","%"+search+"%");
            criteria.andLike("orderState","等待系统分配");
            example.orderBy("orderId");
            Example example1 = new Example(Torder.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.orLike("orderNumber",search);
            criteria1.andLike("transferPhyId", "%/"+String.valueOf(phyId)+"/%");
            criteria1.orLike("orderUser","%"+search+"%");
            criteria1.andLike("transferPhyId", "%/"+String.valueOf(phyId)+"/%");
            criteria1.orLike("serveType","%"+search+"%");
            criteria1.andLike("transferPhyId", "%/"+String.valueOf(phyId)+"/%");
            torders = torderMapper.selectByExample(example);
            List<Torder> torders1 = torderMapper.selectByExample(example1);
            for(int i=0;i<torders1.size();i++){
                torders.add(torders1.get(i));
            }

        }else if(orderState == 2){
            Example example = new Example(Torder.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.orLike("orderUser","%"+search+"%");
            criteria.andLike("orderState","陪护员已接单");
            criteria.andLike("phyId", String.valueOf(phyId));
            criteria.orLike("serveType","%"+search+"%");
            criteria.andLike("orderState","陪护员已接单");
            criteria.andLike("phyId", String.valueOf(phyId));
            example.orderBy("orderId");
            torders = torderMapper.selectByExample(example);
        }else if(orderState==3){
            Example example = new Example(Torder.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.orLike("orderUser","%"+search+"%");
            criteria.andLike("orderState","服务中");
            criteria.andLike("phyId", String.valueOf(phyId));
            criteria.orLike("serveType","%"+search+"%");
            criteria.andLike("orderState","服务中");
            criteria.andLike("phyId", String.valueOf(phyId));
            example.orderBy("orderId");
            torders = torderMapper.selectByExample(example);
        }else if(orderState==4){
            Example example = new Example(Torder.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.orLike("orderUser","%"+search+"%");
            criteria.andLike("orderState","已完成");
            criteria.andLike("phyId", String.valueOf(phyId));
            criteria.orLike("serveType","%"+search+"%");
            criteria.andLike("orderState","已完成");
            criteria.andLike("phyId", String.valueOf(phyId));
            example.orderBy("flag");
            torders = torderMapper.selectByExample(example);
        }else if(orderState==5){
            Example example = new Example(Torder.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.orLike("orderUser","%"+search+"%");
            criteria.andLike("orderState","已取消");
            criteria.andLike("phyId", String.valueOf(phyId));
            criteria.orLike("serveType","%"+search+"%");
            criteria.andLike("orderState","已取消");
            criteria.andLike("phyId", String.valueOf(phyId));
            example.orderBy("orderId");
            torders = torderMapper.selectByExample(example);
        }else {
                throw new BusinessException(ExceptionCode.NO_VIPCARD);
        }
        return  torders;
    }
    @Override
    public void  addTorder2(Torder torder) {
        //设置订单生成时间
        torder.setOrderTime(new Date());
        torder.setOrderState("等待系统分配");
        //订单编号生成
        /////////////////////////////////////////////
        //获得一个3位随机数
        StringBuilder code = new StringBuilder();
        Random ran = new Random();
        for (int i = 0; i < 3; i++) {
            code.append(Integer.valueOf(ran.nextInt(10)).toString());
        }
        //生成一个时间字符串:年月日时分秒毫秒
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String format = simpleDateFormat.format(timestamp);
        //前缀+时间+随机数生成订单编码
        torder.setOrderNumber(TORDER_NUM_PRE+format+code.toString());
        torder.setSerialNumber(OrderIdUtil.getOrderId());
        torderMapper.insert(torder);

    }

    @Override
    public List<Torder> select2(Torder torder) {
        Example example =  new Example(Torder.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderState","已完成");
        criteria.andEqualTo("phyId",torder.getPhyId());
        example.orderBy("flag");
        List<Torder> torders = torderMapper.selectByExample(example);
        return torders;
    }


}
